package application;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import universe.Game;

public class MenuTetris implements ActionListener{

	private JFrame oFrame;
	private Game TheGame;

	public MenuTetris(JFrame pFrame, Game game){
		oFrame = pFrame;
		TheGame = game;

		JMenuBar oMenuBar = new JMenuBar();
		JMenu oMenu1 = new JMenu("Partie");
		JMenuItem oM1 = new JMenuItem("Nouvelle");
		JMenuItem oM2 = new JMenuItem("Pause/Continuer");
		JMenuItem oM3 = new JMenuItem("Quitter");
		JMenu oMenu2 = new JMenu("Paramètres");
		JMenuItem oM21 = new JMenuItem("Configurer");
		JMenu oMenu3 = new JMenu("Aide");
		JMenuItem oM31 = new JMenuItem("Règles du jeu");
		JMenuItem oM32 = new JMenuItem("A propos");

		oM1.addActionListener(this);
		oM2.addActionListener(this);
		oM3.addActionListener(this);
		oM21.addActionListener(this);
		oM31.addActionListener(this);
		oM32.addActionListener(this);


		oMenu1.add(oM1);
		oMenu1.add(oM2);
		oMenu1.add(oM3);
		oMenu2.add(oM21);
		oMenu3.add(oM31);
		oMenu3.add(oM32);
		oMenuBar.add(oMenu1);
		oMenuBar.add(oMenu2);
		oMenuBar.add(oMenu3);

		pFrame.setJMenuBar(oMenuBar);	
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		System.out.println("Je rentre en action");
		if (evt.getSource()instanceof JMenuItem){
			// gestion des événements liés aux menus
			String ChoixOption = evt.getActionCommand();
			if (ChoixOption.equals("Nouvelle")) ;//todo
			else if (ChoixOption.equals("Pause/Continuer")) TheGame.togglePause();
			else if (ChoixOption.equals("Quitter")) oFrame.dispose();
			else if (ChoixOption.equals("Configurer")) windowConfigurer(1);		
			else if (ChoixOption.equals("Règles du jeu")) windowConfigurer(2);
			else if (ChoixOption.equals("A propos")) windowConfigurer(3);
		}
	} 

	private void windowConfigurer(int x){

		boolean pause = TheGame.getPause();
		if(!pause) TheGame.togglePause();
	
		JDialog oDialog = new JDialog();

		oDialog.setSize(new Dimension(500,300));
		oDialog.setLocationRelativeTo(null);
		if(x == 1){
			oDialog.setTitle("Configuration");
			oDialog.setContentPane(new Configuration());
		}else if(x == 2){
			oDialog.setTitle("Règles du jeu");
			oDialog.setContentPane(new Regles());
		}else if(x == 3){
			oDialog.setTitle("A propos");
			oDialog.setContentPane(new Apropos());
		}
		oDialog.setModal(true);
		oDialog.setVisible(true);

		if(!pause) TheGame.togglePause();	

	}

}
